package com.base.hong.ui.main

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.base.hong.databinding.FragmentMineBinding
import com.base.hong.ui.TestActivity
import com.base.hong.ui.base.BaseLazyFragment
import com.base.hong.ui.main.viewmodel.MineViewModel
import com.base.hong.widget.CircleCountDownView
import com.gyf.immersionbar.ImmersionBar
import com.htt.base_library.util.startActivity
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import java.lang.Math.abs


class MineFragment : BaseLazyFragment<MineViewModel, FragmentMineBinding>() {

    private var collapsingToolBarStateFlow =
        MutableStateFlow(CollapsingToolBarState.EXPANDED)

    override fun initView(view: View, savedInstanceState: Bundle?) {
        super.initView(view, savedInstanceState)
        ImmersionBar.with(this)
            .statusBarView(mViewBinding.toolbarLine)
            .statusBarDarkFont(true, 0.2f)
            .init()
        mViewBinding.appBarLayout.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
            when {
                verticalOffset == 0 -> collapsingToolBarStateFlow.value =
                    CollapsingToolBarState.EXPANDED
                abs(verticalOffset) >= appBarLayout.totalScrollRange -> collapsingToolBarStateFlow.value =
                    CollapsingToolBarState.COLLAPSED
                else -> collapsingToolBarStateFlow.value =
                    CollapsingToolBarState.INTERMEDIATE
            }
        }

    }

    override fun setEventListener() {
        lifecycleScope.launch {
            collapsingToolBarStateFlow
                .distinctUntilChanged { old, new ->
                    old == new
                }.collectLatest {
                    if (it == CollapsingToolBarState.COLLAPSED) {
                        mViewBinding.collapsingToolbarLayout.title = "个人中心"
                    } else mViewBinding.collapsingToolbarLayout.title = ""
                }
        }
        mViewBinding.settingFabIcon.setOnClickListener {
            startActivity<TestActivity>()
        }
    }

    override fun lazyLoad() {
    }


}